# -*- coding: UTF-8 -*-
import urllib
import urllib2
import re
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
page = 10
url = 'http://www.xicidaili.com/nn/' + str(page)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
    request = urllib2.Request(url,headers = headers)
    response = urllib2.urlopen(request)
    content = response.read().decode('utf-8')
    pattern = re.compile('<tr.*?class.*?<td.*?class="country".*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<div.*?bar_inner (.*?)".*?<div.*?bar_inner (.*?)"',re.S)
    items = re.findall(pattern,content)
    a=open('E:/data.txt', 'a')
    for item in items:
            if item[2] == 'fast' and item[3] == 'fast':
                a.write(item[0]+"\t"+item[1]+"\n")
                print item[0],item[1],item[2],item[3]
    a.close()
except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason
